// 验证用户名
(()=>{
  //验证各项
  //通过id获取用户输入的用户名
  let uname = document.getElementById("uname");
  uname.onblur = ()=>{
    let unameTip = document.getElementById("unameTip");
    if(!uname.value) {
      unameTip.style.color = "red";
      unameTip.innerHTML = "用户名不能为空";
    }else {
      if(uname.value.length<3) {
        unameTip.style.color = "red";
        unameTip.innerHTML = "用户名不能少于3位";
      }else {
        //任务2.发送请求验证用户名是否存在
        //1.创建对象
        let xhr = new XMLHttpRequest();
        //2.指定接收数据函数
        xhr.onreadystatechange = ()=> {
          //5.接收数据
          if(xhr.readyState==4 && xhr.status==200) {
            let obj=JSON.parse(xhr.responseText);
            // console.log(obj);
            // alert(rs);
            if(!obj.code) {
              unameTip.style.color = "green";
              unameTip.innerHTML = "用户名可用";
            }else {
              unameTip.style.color = "red";
              unameTip.innerHTML = "该用户名已被注册";
            }
          }
        }
        //3.指定服务器地址
        let url =`http://127.0.0.1:8848/user/v1/search?uname=${uname.value}`;
        xhr.open("GET",url,true);
        //4.发送请求
        xhr.send();
      }
    }
  }
  uname.onfocus = ()=>{
    let unameTip = document.getElementById("unameTip");
    unameTip.style.color = "#fff";
    unameTip.innerHTML = "用户名的长度在3~6之间";
  }
})();
//验证密码
(()=>{
  //验证各项
  //通过id获取用户输入的密码
  let upwd = document.getElementById("upwd");
  upwd.onblur = ()=>{
    let upwdTip = document.getElementById("upwdTip");
    if(!upwd.value) {
      upwdTip.style.color = "red";
      upwdTip.innerHTML = "密码不能为空";
    }else {
      if(upwd.value.length<9) {
        upwdTip.style.color = "red";
        upwdTip.innerHTML = "密码不能少于9位";
      }else {
        upwdTip.style.color = "green";
        upwdTip.innerHTML = "密码可用";
      }
    }
  }
  upwd.onfocus = ()=>{
    let upwdTip = document.getElementById("upwdTip");
    upwdTip.style.color = "#fff";
    upwdTip.innerHTML = "密码的长度在9~16之间";
  }
  // 通过id获取用户输入的确认密码并验证
  let confirmPwd = document.getElementById("confirmPwd");
  confirmPwd.onblur = ()=>{
    let confirmPwd = document.getElementById("confirmPwd");
    if(!confirmPwd.value) {
      confirmPwdTip.style.color = "red";
      confirmPwdTip.innerHTML = "确认密码不能为空";
    }else {
      if(confirmPwd.value.length<9) {
        confirmPwdTip.style.color = "red";
        confirmPwdTip.innerHTML = "确认密码不能少于9位";
      }else {
        if(confirmPwd.value!=upwd.value) {
          confirmPwdTip.style.color = "red";
          confirmPwdTip.innerHTML = "确认密码和登录密码不一致";
        }else {
          confirmPwdTip.style.color = "green";
          confirmPwdTip.innerHTML = "密码可用";
        }
      }
    }
  }
  confirmPwdTip.onfocus = ()=>{
    let confirmPwdTip = document.getElementById("confirmPwdTip");
    confirmPwdTip.style.color = "#fff";
    confirmPwdTip.innerHTML = "密码的长度在9~16之间";
  }
})();
// 验证邮箱格式
(()=>{
  let email = document.getElementById("email");
  email.onblur = ()=>{
    let emailTip = document.getElementById("emailTip");
    let reg = new RegExp("^[a-z1-9]\\w{1,}@[a-z1-9]{2,5}\.[a-z]{3,5}$","i");
    // console.log(reg.test(email.value));
    if(!email.value){
      emailTip.style.color = "red";
      emailTip.innerHTML = "邮箱不能为空";
    }else {
      if(!reg.test(email.value)) {
        emailTip.style.color = "red";
        emailTip.innerHTML = "邮箱格式不正确";
      }else {
        emailTip.style.color = "green";
        emailTip.innerHTML = "邮箱可用";
      }
    }
    // console.log(reg);
  }
  email.onfocus = ()=>{
    let emailTip = document.getElementById("emailTip");
    emailTip.style.color = "#fff";
    emailTip.innerHTML = "请输入合法的邮箱地址";
  }
})();
// 验证验证码
(()=>{
  let nextCode = document.getElementById("nextCode");
  nextCode.onclick = ()=>{
    let codeImg = document.getElementById("codeImg");
    // alert(1);
    codeImg.src = `http://127.0.0.1:8848/user/v1/code?t=${new Date().getTime()}`;
  }
  let code = document.getElementById("code");
  code.onblur = ()=>{
    let codeTip = document.getElementById("codeTip");
    if(!code.value){
      codeTip.style.color = "red";
      codeTip.innerHTML = "验证码不能为空";
    }else {
      codeTip.innerHTML = "";
    }
  }
  code.onfocus = ()=>{
    let codeTip = document.getElementById("codeTip");
    codeTip.style.color = "#fff";
    codeTip.innerHTML = "";
  }
})();
//注册
(()=>{
  let btn = document.getElementById("btn");
  btn.onclick = ()=>{
    //1.通过id获取用户输入用户名、密码、邮箱、验证码
    let code = document.getElementById("code");
    let uname = document.getElementById("uname");
    let upwd = document.getElementById("upwd");
    let email = document.getElementById("email");
    //1.创建对象ajax
    let xhr = new XMLHttpRequest();
    //2.指定接收数据的函数
    xhr.onreadystatechange = ()=>{
      if(xhr.readyState===4 && xhr.status===200){
        // console.log(xhr.responseText);
        let codeTip = document.getElementById("codeTip");
        if(xhr.responseText == -1){
          codeTip.style.color = "red";
          codeTip.innerHTML = "验证码格式错误";
        }else if(xhr.responseText == 1){
          location.href = "http://127.0.0.1:8848/html/login.html";
        }
      }
    }
    //3.指定服务器的地址
    let url = `http://127.0.0.1:8848/user/v1/reg`;
    xhr.open("POST",url,true);
    //4.修改请求头信息
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    //5.发送请求
    xhr.send(`uname=${uname.value}&upwd=${upwd.value}&email=${email.value}&code=${code.value}`);
  }
})();